﻿using AG.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AGMapEditor.Windows
{
    public partial class AGEditSwitchPointWindow : Form
    {
        private AGMap _map;

        public AGMapSwitchPoint EditedSwitchPoint { get; private set; }

        public AGEditSwitchPointWindow(AGMap map, AGMapSwitchPoint switchPoint)
        {
            InitializeComponent();

            _map = map;
            EditedSwitchPoint = switchPoint;
            _ctlEditSrcMap.Text = _map.Name;
            _ctlEditId.Text = EditedSwitchPoint.Id.ToString();
            _ctlEditSrcRangeRow.Text = EditedSwitchPoint.Range.X.ToString();
            _ctlEditSrcRangeCol.Text = EditedSwitchPoint.Range.Y.ToString();
            _ctlEditSrcRangeRowCount.Text = EditedSwitchPoint.Range.Width.ToString();
            _ctlEditSrcRangeColCount.Text = EditedSwitchPoint.Range.Height.ToString();

            _ctlBtnTargetMap.Text = EditedSwitchPoint.TargetMapCaption;
            _ctlBtnTargetMap.Tag = EditedSwitchPoint.TargetMapId;
            _ctlEditTargetRow.Text = EditedSwitchPoint.TargetPosRow.ToString();
            _ctlEditTargetCol.Text = EditedSwitchPoint.TargetPosCol.ToString();
        }

        private void _ctlBtnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(_ctlEditId.Text);
                int srcRangeRow = Convert.ToInt32(_ctlEditSrcRangeRow.Text);
                int srcRangeCol = Convert.ToInt32(_ctlEditSrcRangeCol.Text);
                int srcRangeRowCount = Convert.ToInt32(_ctlEditSrcRangeRowCount.Text);
                int srcRangeColCount = Convert.ToInt32(_ctlEditSrcRangeColCount.Text);

                int targetMapId = Convert.ToInt32(_ctlBtnTargetMap.Tag);
                int targetPosRow = Convert.ToInt32(_ctlEditTargetRow.Text);
                int targetPosCol = Convert.ToInt32(_ctlEditTargetCol.Text);

                EditedSwitchPoint.Range = new Rectangle(srcRangeRow, srcRangeCol, srcRangeRowCount, srcRangeColCount);
                EditedSwitchPoint.TargetMapId = (ushort)targetMapId;
                EditedSwitchPoint.TargetPosRow = (ushort)targetPosRow;
                EditedSwitchPoint.TargetPosCol = (ushort)targetPosCol;

                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch
            {
            }
        }

        private void _ctlBtnTargetMap_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            AGSelectMapWindow window = new AGSelectMapWindow(_map);
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _ctlBtnTargetMap.Text = window.SelectedMap.Name;
                _ctlBtnTargetMap.Tag = window.SelectedMap.Id;
            }
        }
    }
}
